package com.kfm.mybatis.mapper;

import com.kfm.mybatis.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserMapper {
    @Select("select * from user")
    @Results(id = "result", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
    })
    List<User> selectAll();

    @Select("select * from user where id = #{id}")
    User selectById(Integer id);



    @Select("select * from user  where id = #{id}")
    @Results(id = "userWithAccounts", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "accounts", column = "id", many = @Many(select = "com.kfm.mybatis.mapper.IAccountMapper.selectByUid"))
    })
    User selectByIdWithAccount(Integer id);


    /**
     * 根据用户 id 查询用户信息及其所有账户信息
     * @param id 用户 id
     * @return
     */
    @Select("select u.id user_id, u.username, u.password, a.id, a.uid, a.money from user u left join account a on u.id = a.uid where u.id = #{id}")
    @Results(id = "userWithAccounts2", value = {
            @Result(id = true, property = "id", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "accounts", many = @Many(resultMap = "com.kfm.mybatis.mapper.IAccountMapper.result"))
    })
    User selectByIdWithAccount2(Integer id);
}
